package com.tseacher.dao.impl;

import com.tseacher.dao.UserDao;
import com.tseacher.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public User selectUser(String email) {
        String sql = "select * from tb_user where email=?";
        List<User> userList = jdbcTemplate.query(sql, new Object[]{email}, new BeanPropertyRowMapper<>(User.class));
        if(userList.size()>0){
            return userList.get(0);
        }
        return null;
    }

    @Override
    public void add(User user) {
        String sql = "insert into tb_user (email,password) value (?,?)";
        jdbcTemplate.update(sql,new Object[]{user.getEmail(),user.getPassword()});
    }
}
